package com.jacen.aws.util;

import com.jacen.aws.entity.KeyEntity;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;

/**
 * 版权声明：Copyright(c) 2018 UCAR Inc. All Rights Reserved.
 *
 * @Author Jacen(liangjie.cai @ ucarinc.com)
 * @Date 2018/10/14 15:43
 * @Version 1.0
 * @Description aws配置读取
 */
public class KeyUtil {

    /**
     * 读取配置
     *
     * @return
     */
    public static HashMap<String, KeyEntity> getKeyMap() {
        HashMap<String, KeyEntity> map = new HashMap(2);
        try {
            Properties pps = new Properties();
            //InputStream in = new BufferedInputStream(new FileInputStream("aws.properties"));
            InputStream in = KeyUtil.class.getResourceAsStream("/aws.properties");
            pps.load(in);

            String[] nickName = pps.getProperty("aws.ec2.nickName").split(",");
            String[] region = pps.getProperty("aws.ec2.region").split(",");
            String[] instanceId = pps.getProperty("aws.ec2.instanceId").split(",");
            String[] secretKey = pps.getProperty("aws.ec2.secretKey").split(",");
            String[] accessKey = pps.getProperty("aws.ec2.accessKey").split(",");

            KeyEntity entity = null;
            for (int i = 0; i < region.length; i++) {
                entity = new KeyEntity();
                entity.setRegion(region[i]);
                entity.setNickName(nickName[i]);
                entity.setInstanceId(instanceId[i]);
                entity.setAccessKey(accessKey[i]);
                entity.setSecretKey(secretKey[i]);
                map.put(entity.getRegion(), entity);
            }
            LogUtil.writeLog("读取aws.properties配置文件成功...");
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.writeLog("读取aws.properties配置文件失败...");
        }
        return map;
    }

}
